package groober;

import java.util.logging.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.JOptionPane;
import rabus.*;

public class Main {

    public static void main(String[] args) {

        final TrayIcon trayIcon;
        final String version = "pre-alpha";
        
        if (SystemTray.isSupported()) {

            // Tworzenie procesu uruchomieniowego ezstream i jego konfiguracja
            String binary, dir;
            if(SysOp.isWindows()) {
                binary = "C:\\Program Files (x86)\\ezstream\\ezstream.exe";
                dir = "C:\\Program Files (x86)\\ezstream\\";
            } else {
                binary = "ezstream";
                dir = "./";
            }
            final ProcessBuilder pb = new ProcessBuilder(binary, "-c", "config.xml");
            pb.directory(new File(dir));
                 
            // Tworzenie tray'a systemowego i ikony
            SystemTray tray = SystemTray.getSystemTray();
            Image trayImage = Toolkit.getDefaultToolkit().getImage("icon.png");

            // Menu po naciśnięciu prawego przycisku myszy na tray'u
            PopupMenu trayMenu = new PopupMenu("Groober " + version);
            
            MenuItem startItem = new MenuItem("Start");
            startItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        if(!ProcOp.isRunning("ezstream")) {
                            System.out.println("Starting...");
                            Process p = pb.start();
                        } else {
                            JOptionPane.showMessageDialog(null,"Streaming already started. Cannot start again!","Already started",2);
                            System.err.println("Streaming already started. Cannot start again!");
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });            
            MenuItem stopItem = new MenuItem("Stop");
            stopItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        if(ProcOp.isRunning("ezstream")) {
                            System.out.println("Stopping...");
                            ProcOp.killProcess("ezstream");
                        } else {
                            JOptionPane.showMessageDialog(null,"Nothing to stop. Start something first!","Already stopped",2);
                            System.err.println("Nothing to stop. Start something first!");
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });            
            MenuItem aboutItem = new MenuItem("About");
            aboutItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null,"Lightweight ezstream front-end by Adam Rabiega [rabusek@gmail.com]","About Groober " + version,1);
                }
            });
            MenuItem exitItem = new MenuItem("Exit");
            exitItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        if(ProcOp.isRunning("ezstream")) {
                            System.out.println("Killing processes...");
                            ProcOp.killProcess("ezstream");
                        } else {
                            System.out.println("Nothing to kill...");
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    System.out.println("Exiting...");
                    System.exit(0);
                }
            });
            trayMenu.add(startItem);
            trayMenu.add(stopItem);
            trayMenu.add(aboutItem);
            trayMenu.add(exitItem);

            // Konfiguracja i utworzenie tray'a oraz akcja po podwójnym kliknięciu w tray'a
            trayIcon = new TrayIcon(trayImage, "Groober " + version, trayMenu);
            trayIcon.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage("Groober " + version, 
                        "Still in progress, I guuess ;-)",
                        TrayIcon.MessageType.INFO);
                }
            });
            trayIcon.setImageAutoSize(true);

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.err.println("TrayIcon could not be added. " + e);
            }

        } else {
            System.err.println("System Tray is not supported on your operating system!");
        }
    }
}